<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Service\Curl;
use App\Service\Dir;


class CaijiController extends Controller
{
    // 采集各种网站文章的时候的测试程序

    // 整理下载的模板文件中没有的图片 删除掉
    public function not()
    {
        // 获取所有的文件
        $path  = 'C:\Users\shenlan03\Desktop\官网';
        $dir   = new Dir();
        $files = $dir->readDir($path);


        // 找出非图片文件 后缀为.html .js .css .下载
        $in      = ['html', 'js', 'css', '下载'];
        $find    = [];
        $content = [];
        foreach($files as $key => $_file){
            if(is_array($_file)){
                foreach($files as $_file2){
                    if(is_array($_file2)){
                        foreach($_file2 as $_file3){
                            if(is_array($_file3)){
                                continue;
                            }
                            $res = pathinfo($_file3, PATHINFO_EXTENSION);
                            if(in_array($res, $in)){
                                $content[] = $_file3;
                            } else{
                                $find[] = $_file3;
                            }
                        }
                    } else{
                        $res = pathinfo($_file2, PATHINFO_EXTENSION);
                        if(in_array($res, $in)){
                            $content[] = $_file2;
                        } else{
                            $find[] = $_file2;
                        }
                    }
                }
            } else{
                $res = pathinfo($_file, PATHINFO_EXTENSION);
                if(in_array($res, $in)){
                    $content[] = $_file;
                } else{
                    $find[] = $_file;
                }
            }
        }

        // 获取内容
        $not = $find;
        foreach($content as $_file){
            $get = file_get_contents($_file);

            // 查询文件（没有在模板中使用的文件）
            foreach($not as $_ki => $_fi){
                if(strpos($get, basename($_fi)) !== false){
                    unset($not[$_ki]);
                }
            }
        }

        // 删除不存在的文件
        if($not){
            foreach($not as $_file){
                unlink($_file);
            }
        }

        return $this->success($not);
    }

    // 批量操作
    public function batch()
    {
        $url = 'http://www.cenbel.com/images/wz01.png';
        $url = 'http://www.cenbel.com/img_2019/demo/logo.png';
        $url = 'http://www.cenbel.com/img_2019/icon1.png';
        $pre = 'http://www.cenbel.com/img_2019/icon/5/';
        for($ii = 1; $ii < 100; $ii++){
            //$ii  = $ii > 10 ? $ii : '0'.$ii;
            $url = $pre.'icon5_'.$ii.'.png';
            $res = $this->image($url);
        }

        return $this->success([], '采集完成！');
    }

    // 采集图片
    public function image($url)
    {
        $head = get_headers($url, 1);
        if(strpos($head['Content-Type'], 'image') === false){
            return false;
        }

        // 采集
        $curl = new Curl();
        $res  = $curl->url($url)->request();

        // 保存图片
        $name = basename($url);
        $path = 'C:\\Users\\shenlan03\\Desktop\\fsdownload\\caiji\\';
        file_put_contents($path.$name, $res);

        return true;
    }

    function is_base64($str)
    {
        return $str == base64_encode(base64_decode($str)) ? true : false;
    }

    // 列表页面采集
    public function lists()
    {
        $url  = 'https://www.sjqcj.com/News/index?t=2';
        $url  = $this->getData('url');
        $curl = new Curl();
        $res  = $curl->url($url)->request();

        // 截取需要的内容
        $content = $curl->cut_str($res, '<div class="weiba_postlist">', '<div class="footer">');
        $content = str_replace(array("\r", "\n", "\r\n"), '', $content);

        // 获取请求连接
        $titlePreg = '/<a target="_blank" href="(.*?)">/is';
        preg_match_all($titlePreg, $content, $links);
        $links = $links[1];

        // 翻页
        $pageHtml = $curl->cut_str($content, '<div id="page" class="page" style="">');
        $pagePreg = '/<a href="(.*?)">(.*?)<\/a>/is';
        preg_match_all($pagePreg, $pageHtml, $pages);
        $pages = $pages[1];

        // var_dump($links);
        // var_dump($pages);
        /*foreach($links as $_url){
            $this->caijiBolg($_url);
        }

        return $this->success();*/

        return $this->success($links);
    }

    // 采集文章
    public function getblog()
    {
        //$url = 'https://www.sjqcj.com/news/detail?id=44231';
        //$url = '/news/detail?id=44231';
        //$url = '/news/detail?id=44217';
        $url = $this->getData('url');
        $res = $this->caijiBolg($url);
        if($res == false){
            return $this->error($this->error);
        } else{
            return $this->success([], '采集文章成功！  '.$res);
        }
    }

    // 采集文字
    protected function caijiBolg($url)
    {
        // URL
        //$url = $this->getData('url');
        //$url = '/news/detail?id=44173';
        //$url = '/news/detail?id=44140';


        // 判断数据库中是否已经采集该链接
        // 如果已经采集则不用在采集了
        // $info = DB::table('sjqcj')->where('url', $url)->get()->toArray();
        $db   = new Sjqcj();
        $info = $db->where('url', $url)->get()->toArray();
        if($info){
            $this->error = '该链接已经采集';

            //return $this->error('该链接已经采集');
            //return false;
        }

        // 参数
        $host  = 'https://www.sjqcj.com';
        $full  = $host.$url;
        $arr   = parse_url($full);
        $query = explode('=', $arr['query']);
        $id    = $query[1];

        // 采集
        $curl     = new Curl();
        $res      = $curl->url($full)->request();
        $mainHtml = $curl->cut_str($res, '<div class="content">', '<div class="footer">');
        $mainHtml = str_replace(array("\r", "\n", "\r\n"), '', $mainHtml);

        // 获取标题
        $titlePreg = '/<h3><span style="font-size:16px;color: #1E6BC5;font-weight: bold;">(.*?)<\/span><\/h3>/is';
        preg_match($titlePreg, $mainHtml, $title);

        // 获取时间
        $timePreg = '/<p class="">.*<span class="">(.*?)&nbsp;&nbsp;&nbsp;.*<\/span>.*<\/p>/is';
        preg_match($timePreg, $mainHtml, $time);
        $time = trim($time[1]);
        if(strpos($time, '今天') !== false){
            $time = date('Y-m-d').' '.mb_substr($time, 2);
            $time = strtotime($time);
        } elseif(strpos($time, '分钟前') !== false){
            $time = strtotime(-mb_substr($time, 0, -3).' min');
        } else{
            $time = str_replace('月', '-', $time);
            $time = str_replace('日', ' ', $time);
            $time = date('Y-').$time;
            $time = strtotime($time);
        }

        // 获取内容
        //$contentPreg = '/<div class="pb15 weiba-article" id=".*">(.*?)<\/div>/is';
        //preg_match($contentPreg, $mainHtml, $content);

        // 解析url 获取内容
        $start   = '<div class="pb15 weiba-article" id="'.$id.'">';
        $content = $curl->cut_str($mainHtml, $start, '</div>');
        $content = trim($content);
        $length  = mb_strlen($content);
        // 大于200字 不要
        if($length >= 600){
            $this->error = '该链接文章字数大于600字';

            //return $this->error('该链接文章字数大于600字！');
            return false;
        }
        // 有图片不要
        if(strpos($content, '<img') !== false){
            $this->error = '该链接文章含有图片';

            //return $this->error('该链接文章含有图片！');
            return false;
        }
        // 判断内容中是否有div
        if(strpos($content, '<div') === 0){
            $content = $content.'</div>';
        }

        // 保存数据库
        $title = $title[1];
        $data  = ['title' => $title, 'content' => $content, 'url' => $url, 'date' => $time];

        // 保存数据
        $db->fill($data);
        $result = $db->save();

        //DB::table('sjqcj')->insert($data);

        return $db->id;
        // return $this->success([], '采集文章成功！  '.$db->id);
    }


}
